<?php
/***************************************************************************
 *
 *   copyright            : (C) 2005 Winds of Storm
 *
 *   $Id: avatars.php,v 1.21 2007/11/08 20:39:07 amorgan Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

require("config.php");
$body['class']	= 'row3';
$body['id']	= 'avatars';
$skip_header	= 1;
require($libdir . "init." . $phpEx);
require($libdir . "chat_functs." . $phpEx);

function parse_user_avatar($uinfo, $show_avatars, $template) {
	global $profile_url;

	$away	= "";
	if ($uinfo['user_away'] == 1) {	
		$away	= " <i>(away)</i>";
	}

	$template->assign_block_vars('ismod.user', array(
		'ALIAS'		=> $uinfo['user_alias'],
		'AWAY'		=> $away,
		'U_PROFILE'	=> getProfileURL($uinfo)
	));

	if ($show_avatars == 0) {
		if ($uinfo['avatar_url'] != "" && $uinfo['avatar_url'] != "noavatar") {
			$template->assign_block_vars('ismod.user.avatar', array(
				'AVATAR_URL'	=> $uinfo['avatar_url'],
				'AVATAR_TITLE'	=> $uinfo['username']
			));
		}
	}

}

$sid	= $_REQUEST['sid'];
$sql	= "SELECT *
		FROM
			{$dbprefix}sessions AS s,
			{$dbprefix}prefs AS p
		WHERE	
			s.session_id='$sid' AND
			s.user_id=p.user_id";
$result	= $db->sql_query($sql);
$rows	= $db->sql_numrows($result);

if ($rows == 0) {
	showFatalError("Session<br />Expired.");
}

$p_row	= $db->sql_fetchrow($result);
$room	= $p_row['room_id'];

$sql	= "SELECT *
		FROM
			{$dbprefix}sessions AS s,
			{$dbprefix}prefs AS p
		WHERE
			room_id=$room AND
			s.user_id=p.user_id
		ORDER BY
			moderator,
			user_alias";
		//GROUP BY
			//p.user_id
$result = $db->sql_query($sql);
$rows	= $db->sql_numrows($result);

if ($rows == 0) {
	showFatalError("Session<br />Expired.");
}

if ($_REQUEST['cpsp'] == MODE_SLIMCHAT) {
	$header[]['HEADER_TEXT']	= "<meta http-equiv=refresh content='60; url=avatars.$phpEx?sid={$_REQUEST['sid']}&cpsp=" . MODE_SLIMCHAT . "'>\n";
}
setDomainText(__FILE__, &$header, $_REQUEST['cpsp']);
showHeader($body, $header);

$template = new Template($config['TPL_DIR']);
$template->set_filenames(array('body' => 'avatar_body.tpl'));

$moderators	= array();
$members	= array();
$mod_count	= 0;
$mem_count	= 0;

while ($row = $db->sql_fetchrow($result)) {
	if ($row['moderator']) {
		$moderators[$row['user_id']]	= $row;
		$mod_count++;
	} else {
		$members[$row['user_id']]	= $row;
		$mem_count++;
	}
}

if ($mod_count > 0) {
	$template->assign_block_vars('ismod', array(
		'NAME'	=> "Moderators"
	));
	foreach ($moderators as $mod_name => $modinfo) {
		parse_user_avatar($modinfo, $p_row['user_show_avatars'], &$template);
	}
}
if ($mem_count > 0) {
	$template->assign_block_vars('ismod', array(
		'NAME'	=> "Members"
	));
	foreach ($members as $mem_name => $meminfo) {
		parse_user_avatar($meminfo, $p_row['user_show_avatars'], &$template);
	}
}

$template->pparse('body');
